package org.shopen.puremvc.model
{
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.shopen.puremvc.model.domain.theory.Stage;

	public class StagesProxy extends Proxy
	{
		public static const NAME : String = "StagesProxy";
		public static const STAGES_INITIALIZED : String = NAME + ".STAGES_INITIALIZED";
		public static const STAGES_META_INITIALIZED : String = NAME + ".STAGES_META_INITIALIZED";
		
		override public function getProxyName():String
		{
			return StagesProxy.NAME;
		}
		
		public function StagesProxy()
		{
			super(NAME);
		}
		
		public function get stages() : ArrayCollection
		{
			var result : ArrayCollection = getData() as ArrayCollection; 
			return result;
		}
		
		public function get stagesMeta() : ArrayCollection
		{
			var result : ArrayCollection = new ArrayCollection();
			for each(var stage : Stage in stages)
			{
				result.addItem(stage.meta);
			}
			
			return result;
		}
		
		
		public function getStage(stageId : String) : Stage
		{
			var result : Stage = null;
			for each(var stage : Stage in stages)
			{
				if (stage.meta.id == stageId)
				{
					result = stage;
					break;
				}
			}
			return result;
		}
		
		public function initData(node : XML) : void
		{
			var result : ArrayCollection = new ArrayCollection();
			var stageNodes : XMLList = node.stage as XMLList;
			for each (var stageNode : XML in stageNodes)
			{
				var stage : Stage = new Stage(stageNode);
				result.addItem(stage);
			}
			setData(result);
			sendResult();
		}
		
		protected function sendResult() : void
		{
			sendNotification(StagesProxy.STAGES_INITIALIZED, this.stages);
			sendNotification(StagesProxy.STAGES_META_INITIALIZED, this.stagesMeta);
		}
		
		private var currentStageId : String = null;
		public function setCurrentStage(id : String) :void
		{
			currentStageId = id;
		}
		
		public function getCurrentStage() : Stage
		{
			var result : Stage = null;
			result = getStage(currentStageId);			
			return result;
		}
	}
}